package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.domain.User;
import com.shop.service.UserService;
import com.shop.mapper.UserMapper;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author 靳润波
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-08-01 09:59:30
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper iUserMapper;

    @Override
    public List<User> selectAll() {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getIsDelete,0);
        return iUserMapper.selectList(lqw);
    }

    @Override
    public Integer insert(User iUser) {
        Integer i = null;
        try {
            String hashpw = BCrypt.hashpw(iUser.getPassword(), BCrypt.gensalt());
            iUser.setPassword(hashpw);
            i = iUserMapper.insert(iUser);
        } catch (Exception e) {
            return -2;
        }
        return i;
    }

    @Override
    public Integer update(User iUser) {
        return iUserMapper.updateById(iUser);

    }

    @Override
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                User iUser = new User();
                iUser.setId(Long.valueOf(id));
                iUser.setIsDelete(1);
                iUserMapper.updateById(iUser);
            }
            return 1;
        } catch (Exception e) {
            return -1;
        }

    }

    @Override
    public List<User> selectList(User iUser) {
        LambdaQueryWrapper<User> lqw = this.lqw(iUser);
        return iUserMapper.selectList(lqw);

    }

    @Override
    public Integer status(Long id, Integer status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        return iUserMapper.updateById(user);

    }

    @Override
    public void resetPwd(Long id) {
            String hashpw = BCrypt.hashpw("123456", BCrypt.gensalt());
            User user = new User();
            user.setId(id);
            user.setPassword(hashpw);
            int i = iUserMapper.updateById(user);
            if (i != 1) {
                throw new RuntimeException("重置密码失败");
            }
    }

    @Override
    public String login(String username, String password) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, username);
        User user = iUserMapper.selectOne(lambdaQueryWrapper);
        if(user == null){
            return "-2";
        }
        if(!BCrypt.checkpw(password, user.getPassword())){
            return "-1";
        }
        if(user.getStatus()==1){
            return "-3";
        }
        Date date = new Date();
        Map map = new HashMap();
        map.put("username",user.getUsername());
        map.put("id",user.getId());
        map.put("role",user.getRole());
        JwtBuilder itbuka = Jwts.builder().setId(IdWorker.get32UUID())
                .setClaims(map)
                .setIssuedAt(date)
                .setExpiration(new Date(date.getTime()+1000*60*60*24*7))
                .signWith(SignatureAlgorithm.HS256,"itbuka");
        String token = itbuka.compact();

        return token;
    }

    public LambdaQueryWrapper lqw(User iUser) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        if (iUser.getId() != null) {
            lqw.eq(User::getId, iUser.getId());
        }
        if (iUser.getUsername() != null) {
            lqw.eq(User::getUsername, iUser.getUsername());
        }
        if (iUser.getAvatar() != null) {
            lqw.eq(User::getAvatar, iUser.getAvatar());
        }
        if (iUser.getPassword() != null) {
            lqw.eq(User::getPassword, iUser.getPassword());
        }
        if (iUser.getPhoneNum() != null) {
            lqw.eq(User::getPhoneNum, iUser.getPhoneNum());
        }
        if (iUser.getEmail() != null) {
            lqw.eq(User::getEmail, iUser.getEmail());
        }
        if (iUser.getNickname() != null) {
            lqw.eq(User::getNickname, iUser.getNickname());
        }
        if (iUser.getDepartment() != null) {
            lqw.eq(User::getDepartment, iUser.getDepartment());
        }
        if (iUser.getRole() != null) {
            lqw.eq(User::getRole, iUser.getRole());
        }
        if (iUser.getStatus() != null) {
            lqw.eq(User::getStatus, iUser.getStatus());
        }
        if (iUser.getIsDelete() != null) {
            lqw.eq(User::getIsDelete, iUser.getIsDelete());
        }
        if (iUser.getCreateTime() != null) {
            lqw.eq(User::getCreateTime, iUser.getCreateTime());
        }
        if (iUser.getUpdateTime() != null) {
            lqw.eq(User::getUpdateTime, iUser.getUpdateTime());
        }
        return lqw;
    }


}




